package com.xxx.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xxx.pojo.Employee;
import com.xxx.util.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MPMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createTime", LocalDateTime.now());
//        System.out.println(BaseContext.getCurrentId());
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        metaObject.setValue("createUser", employee.getId());
        metaObject.setValue("updateUser", employee.getId());

    }

    @Override
    public void updateFill(MetaObject metaObject) {

        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();

        metaObject.setValue("updateTime", LocalDateTime.now());

        metaObject.setValue("updateUser", employee.getId());
    }
}
